﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Ex03.GarageLogic
{
    public class VehicleInGarageCollection
    {
        private List<VehicleInGarage> m_VehicleInGarageList;

        public VehicleInGarageCollection()
        {
            m_VehicleInGarageList = new List<VehicleInGarage>();
        }

        public List<VehicleInGarage> GetAllVehicles()
        {
            return m_VehicleInGarageList;
        }

        public List<VehicleInGarage> GetAllFuelVehicles()
        {
            List<VehicleInGarage> fuelVehicles = new List<VehicleInGarage>();

            foreach (VehicleInGarage vehicleInGarage in m_VehicleInGarageList)
            {
                if (vehicleInGarage.Vehicle is FuelVehicle)
                {
                    fuelVehicles.Add(vehicleInGarage);
                }
            }

            return fuelVehicles;
        }

        public List<VehicleInGarage> GetAllElectricVehicles()
        {
            List<VehicleInGarage> electricVehicles = new List<VehicleInGarage>();

            foreach (VehicleInGarage vehicleInGarage in m_VehicleInGarageList)
            {
                if (vehicleInGarage.Vehicle is ElectricVehicle)
                {
                    electricVehicles.Add(vehicleInGarage);
                }
            }

            return electricVehicles;
        }

        public List<VehicleInGarage> GetVehiclesByVehicleStatus(eVehicleStatus i_VehicleStatus)
        {
            List<VehicleInGarage> vehicles = new List<VehicleInGarage>();

            foreach (VehicleInGarage vehicleInGarage in m_VehicleInGarageList)
            {
                if (vehicleInGarage.VehicleStatus == i_VehicleStatus)
                {
                    vehicles.Add(vehicleInGarage);
                }
            }

            return vehicles;
        }

        public VehicleInGarage GetVehicleByLicensingNumber(string i_LicensingNumber)
        {
            VehicleInGarage returnVehicleInGarage = null;
            Vehicle vehicle = new ElectricCar(string.Empty, i_LicensingNumber, null, 0, eColor.Blue, eDoorsCount.Two);

            foreach (VehicleInGarage vehicleInGarage in m_VehicleInGarageList)
            {
                if (vehicleInGarage.Vehicle.Equals(vehicle))
                {
                    returnVehicleInGarage = vehicleInGarage;
                    break;
                }
            }

            if (returnVehicleInGarage == null)
            {
                throw new VehicleNotFoundException(i_LicensingNumber);
            }

            return returnVehicleInGarage;
        }

        public bool VehicleExists(Vehicle i_Vehicle)
        {
            bool retVal = false;

            foreach (VehicleInGarage vehicleInGarage in m_VehicleInGarageList)
            {
                if (vehicleInGarage.Vehicle.Equals(i_Vehicle))
                {
                    retVal = true;
                    break;
                }
            }

            return retVal;
        }

        public void AddVehicle(Vehicle i_Vehicle, VehicleOwner i_VehicleOwner)
        {
            VehicleInGarage vehicleInGarage = new VehicleInGarage(i_Vehicle, i_VehicleOwner, eVehicleStatus.Repairing);
            m_VehicleInGarageList.Add(vehicleInGarage);
        }
    }
}